Список содержимого

Linux Programming tips

I’ve made this notes while reading Advanced Linux Programming

Some general info

man

  • (1) User commands
  • (2) System calls
  • (3) Standard library functions
  • (8) System/administrative commands

man -k keyword

Emacs

  • open a file: C-x C-f
  • save a file: C-x C-s
  • exit Emacs: C-x C-c
  • highlight on: (global-font-lock-mode t)
  • debug: M-x gdb

Programming

GCC

  • create .o file: gcc -c
  • include path: -I
  • define macros: -D NAME=value
  • optimization: -O2
  • library path: -L
  • library name: -l
  • executable: -o ex.name files.o
  • with debug info: -g

make

target: depends on
	 command
clean:
	rm -f *.o ex.name

Debugging

gdb
  • stack trace: where
  • n levels up in trace: up n
  • print variable
  • break funcname
  • run args
  • next, step
Other tools

The strace command traces the execution of another program, listing any system calls the program makes and any signals it receives.

Analyze programs

If you invoke GCC with -Wall and -pedantic, the compiler issues warnings about risky or possibly erroneous programming constructions.

Profiling

The first step in profiling a program is to annotate its executable to collect profiling information. To do so, use the -pg compiler flag when both compiling the object files and linking. Then use gprof to display profiling results

Parsing command line options

GNU Coding Standards on User Interfaces info “(standards)User Interfaces”

parse command line options: getopt_long

Tempfiles

create temp file: mkstemp (sytem call) next call unlink, the name of the file will disappear, but the content will stay until file closed, so there is no need to delete file manually

Create temp file: tmpfile (stdlib)

Libraries

Static

% ar cr libtest.a test1.o test2.o

The cr flags tell ar to create the archive.

Linking into application: % gcc -static -o app app.o -L. –ltest

Shared

% gcc -c -fPIC test1.c

The -fPIC option tells the compiler that you are going to be using test.o as part of a shared object.

Then you combine the object files into a shared library, like this: % gcc -shared -fPIC -o libtest.so test1.o

Linking is the same way.

The ldd command displays the shared libraries that are linked into an executable.

Dynamic loading

dlopen (”libtest.so”, RTLD_LAZY)

To use dynamic loading functions, include the <dlfcn.h> header file and link with the –ldl option to pick up the libdl library.

void* handle = dlopen (”libtest.so”, RTLD_LAZY); void (*test)() = dlsym (handle, “my_function”); (*test)(); dlclose (handle);

Processes

getpid() returns pid

getppid() returns parent pid

List of processes in brief form

% ps -e -o pid,ppid,command

-f (full listing), -l (long listing), or -j (jobs listing)

priority

% nice -n 10 sort input.txt > output.txt

You can use the renice command to change the niceness of a running process from the command line.

ulimit shell command enables you to restrict the resource usage of programs you run

exec
  • Functions that contain the letter p in their names (execvp and execlp) accept a program name and search for a program by that name in the current execution path; functions that don’t contain the p must be given the full path of the program to be executed.
  • Functions that contain the letter v in their names (execv, execvp, and execve) accept the argument list for the new program as a NULL-terminated array of pointers to strings. Functions that contain the letter l (execl, execlp, and execle) accept the argument list using the C language’s varargs mechanism.
  • Functions that contain the letter e in their names (execve and execle) accept an additional argument, an array of environment variables. The argument should be a NULL-terminated array of pointers to character strings. Each character string should be of the form “VARIABLE=value”
IPC

The ipcs command provides information on interprocess communication facilities, including shared segments. Use the -m flag to obtain information about shared memory.

If this memory segment was erroneously left behind by a program, you can use the ipcrm command to remove it.

On pipes

For example, to redirect a process’s standard input to a file descriptor fd, use this line: dup2 (fd, STDIN_FILENO);

The call to popen creates a child process executing the sort command, replacing calls to pipe, fork, dup2, and execlp.

fifo

% mkfifo /tmp/fifo

The first character of the output from ls is p, indicating that this file is actually a FIFO (named pipe). In one window, read from the FIFO by invoking the following:

% cat < /tmp/fifo

write:

% cat > /tmp/fifo

Terminals

Linux creates a PTY for every new terminal window you open and displays a corresponding entry in /dev/pts. The PTY device acts like a terminal device—it accepts input from the keyboard and displays text output from the programs that run in it. PTYs are numbered, and the PTY number is the name of the corresponding entry in /dev/pts. You can display the terminal device associated with a process using the ps command. Specify tty as one of the fields of a custom format with the -o option.

/proc

General information % man 5 proc

What can be found in /proc

chroot

The root directory for a process can be changed using the chroot call or the chrootcommand. [2]

cpuid

See the documentation of the cpuid instruction in Intel’s IA-32 Intel Architecture Software Developer’s Manual, Volume 2: Instruction Set Reference. This manual is available from http://developer.intel.com/design To read more about the x86 instruction set, which we will use in this chapter, see http://developer.intel.com/design/pentiumii/manuals/ and http://www.x86-64.org/documentation.

hostname

The /proc/sys/kernel/hostname and /proc/sys/kernel/domainname entries contain the computer’s hostname and domain name, respectively. This information is the same as that returned by the uname system call.

Misc

Disc usage info: % df -h /tmp/virtual-fs Filesystem Size Used Avail Use% Mounted on /tmp/disk-image 9.7M 13k 9.

Coding techniques

use assert liberally throughout your programs. gcc option to exclude asserts in release: -DNDEBUG

use htons to convert the port number to network byte order

Some useful system calls

A listing of system calls for your version of the Linux kernel is in /usr/include/asm/unistd.h.

gettimeofday (&tv, &tz); Returns time

The mlock family of system calls allows a program to lock some or all of its address space into physical memory. This prevents Linux from paging this memory to swap space, even if the program hasn’t accessed it for a while. Works only for root proccesses.

Sendfile The sendfile system call provides an efficient mechanism for copying data from one file descriptor to another. The file descriptors may be open to disk files, sockets, or other devices.

 
projects/alp_tips.txt · Последние изменения: 2007/04/12 21:40 anton
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki